********Figure 5********

*Specification chart program
clear
cap program drop specchart
program specchart
syntax varlist, [replace] [sizerep] spec(string)
	* save current data
	tempfile temp
	save "`temp'",replace
	*dataset to store estimates
	*sizerep is for when you want this for sizerep
	if "`replace'"!=""{
			clear
			gen beta=.
			gen se=.
			gen spec_id=.
			gen u95=.
			gen u90=.
			gen l95=.
			gen l90=.
			save "estimates.dta",replace
	}	
	else{
		* load dataset
		use "estimates.dta",clear
	}
	
	if "`sizerep'"=="" {
	* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta =_b[`varlist'] if  spec_id==`obs'
	replace se=_se[`varlist']   if  spec_id==`obs'
	replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
	}
	else {
		* add observation
	local obs=_N+1
	set obs `obs'
	replace spec_id=`obs' if _n==`obs'
	* store estimates
	replace beta = _b[`varlist'] if  spec_id==`obs'
	replace se=e(N)   if  spec_id==`obs'
	*replace u95=beta+invt(e(df_r),0.975)*se if  spec_id==`obs'
	*replace u90=beta+invt(e(df_r),0.95)*se if  spec_id==`obs'
	*replace l95=beta-invt(e(df_r),0.975)*se  if  spec_id==`obs'
	*replace l90=beta-invt(e(df_r),0.95)*se  if  spec_id==`obs'
	* store specification
	foreach s in `spec'{
		cap gen `s'=1 			if  spec_id==`obs'
		cap replace `s'=1 		 if  spec_id==`obs'
	}
	
	}
	
		save "estimates.dta",replace
	* restore dataset
	use `temp',clear
end

*Load data
cd "$reploc/data/created_data"
use mex_full, clear

sort pid_link mxfls
drop if mxfls==.
keep if panel==1

keep rbuc rbuc_nga rbuc_garefined rbuc_garefinedFULL rbucFULL rbucFULL_nga mxfls pid_link inflation imp_age panel birthyr grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co? grexprovSD_lam0co? birthstate evermig yearprov

egen evermig1=total(evermig), by(pid_link)

reshape wide rbuc rbuc_nga rbuc_garefined rbuc_garefinedFULL rbucFULL rbucFULL_nga  inflation imp_age grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co? grexprovSD_lam0co? evermig, i(pid_link) j(mxfls)

local varlist1 rbuc rbuc_nga rbuc_garefined rbuc_garefinedFULL rbucFULL rbucFULL_nga inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 grexprovAvg_lam0 grexprovSD_lam0 grexprovAvg_lam0co2 grexprovSD_lam0co2 grexprovAvg_lam0co3 grexprovSD_lam0co3 


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'3-`var'2
}

*Generate cohort by birthyear variables to cluster on
gen coh_prov_clust=yearprov

tempfile savimex
save "`savimex'", replace

*Results

local varint Avg SD sizeAvg sizeSD


foreach varinti in `varint' {
use "`savimex'", clear

*Main specification
reg rbuc_ngadif grbrprovAvg_lam0co3dif grbrprovSD_lam0co3dif  inflationdif, vce(cluster coh_prov_clust)    

if "`varinti'"=="Avg" | "`varinti'"=="SD"{ 
specchart grbrprov`varinti'_lam0co3dif,spec(main birthprov yesmigs nofull rbuc_nga oldmexonly) replace
}

if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
local hola=substr("`varinti'",5,.)
specchart grbrprov`hola'_lam0co3dif,spec(main birthprov yesmigs nofull rbuc_nga oldmexonly) replace sizerep
}

*Loop over all alternative specifications
local locs birthprov lifeprov
local rbuccollspecs nofull full
local rbucspecs rbuc rbuc_garefined rbuc_nga
local agespecs oldlike41 oldmextot oldmexonly post41
local migs yesmigs nomigs

 
foreach loci in `locs' {

if "`loci'"=="lifeprov" {
local usei grex
}

if "`loci'"=="birthprov" {
local usei grbr
}

foreach migi in `migs' {



if "`migi'"=="nomigs" {
drop if evermig1>0 
}

foreach collspeci in `rbuccollspecs' {

foreach rbucspeci in `rbucspecs' {

if "`collspeci'"=="nofull" {
local rbucuse `rbucspeci'
}

if "`collspeci'"=="full" {

if "`rbucspeci'"=="rbuc" {
local rbucuse rbucFULL
}


if "`rbucspeci'"=="rbuc_garefined" {
local rbucuse rbuc_garefinedFULL
}

if "`rbucspeci'"=="rbuc_nga" {
local rbucuse rbucFULL_nga 
}
}

foreach agespeci in `agespecs' {

preserve


if "`agespeci'"=="oldmexonly" {
local rhsvaruse co3
}

if "`agespeci'"=="post41" {
keep if birthyr>=1941
local rhsvaruse
}


if "`agespeci'"=="oldlike41" {
local rhsvaruse
}

*di "`loci'" "`rbucspeci'" "`agespeci'"

qui: reg `rbucuse'dif  `usei'provAvg_lam0`rhsvaruse'dif `usei'provSD_lam0`rhsvaruse'dif  inflationdif, vce(cluster coh_prov_clust) 

if "`varinti'"=="Avg" | "`varinti'"=="SD"{ 
specchart `usei'prov`varinti'_lam0`rhsvaruse'dif,spec(alter `loci' `migi' `collspeci' `rbucspeci' `agespeci')
}

if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
local hola=substr("`varinti'",5,.)
specchart `usei'prov`hola'_lam0`rhsvaruse'dif,spec(alter `loci' `migi' `collspeci' `rbucspeci' `agespeci') sizerep
}

  

restore

}
}
}
}
}



*Create chart
use "estimates.dta",clear

*Drop duplicate of main
duplicates drop  oldlike41 oldmextot oldmexonly post41 rbuc rbuc_garefined rbuc_nga nofull full yesmigs nomigs birthprov lifeprov, force

gen locicat=1 if birthprov==1
replace locicat=2 if lifeprov==1

gen migicat=1 if yesmigs==1
replace migicat=2 if yesmigs==2

gen collspecicat=1 if nofull==1
replace collspecicat=2 if full==1

gen rbuccat=1 if rbuc==1
replace rbuccat=2 if rbuc_garefined==1
replace rbuccat=3 if rbuc_nga==1

gen oldcat=1 if oldmexonly==1
replace oldcat=2 if post41==1 
replace oldcat=3 if oldlike41==1

sort beta
gen rank=_n

	local scoff=" "
	local scon=" "
	
if "`varinti'"=="Avg" {
	local ind=-2.15
	}
	
	if "`varinti'"=="SD" {
	local ind=-0.5
	}
	
			if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {
	local ind=-1000
	}
	
	foreach var in  oldlike41 oldmextot oldmexonly post41 rbuc rbuc_garefined rbuc_nga nofull full yesmigs nomigs birthprov lifeprov {
	

	
	   cap gen i_`var'=`ind'
	   
	 *Leave space for each new category to put names
	   
	   if "`varinti'"=="Avg" | "`varinti'"=="SD" {
	   if  "`var'"=="post41" |  "`var'"=="rbuc_nga" | "`var'"=="full" | "`var'"=="nomigs" {
	   local ind=`ind'-0.3
	   }
	   
	   else {
	   local ind=`ind'-0.15
	   }
	   }
	   else {
	   	   if  "`var'"=="post41" |  "`var'"=="rbuc_nga" | "`var'"=="full" | "`var'"=="nomigs" {
	   local ind=`ind'-2000
	   }
	   
	   else {
	   local ind=`ind'-1000
	   }
	   }
	  local scoff="`scoff' (scatter i_`var' rank if `var'==., msymbol(circle_hollow) msize(vsmall) mcolor(gs10))" 
	  local scon="`scon' (scatter i_`var' rank if `var'==1, msymbol(X) msize(small) mcolor(black))" 
      local scon="`scon' (scatter i_`var' rank if `var'==1 & main==1, msymbol(X) msize(small) mcolor(orange))" 

	}



*Plot

if "`varinti'"=="Avg" {

tw  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) barwidth(0.1) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) barwidth(0.1) lwidth(none))  /// 90% CI
   (scatter beta rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
   `scoff' `scon' /// indicators for spec
  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
,legend (order(1 "Preferred spec." 4 "Point estimate" 2 "95% CI" 3 "90% CI") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") ///
   yscale(noline) xscale(noline) ylab(-1.75(0.5)0, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) yline(0, lcolor(red))
   
   
   
   gr_edit .yaxis1.add_ticks -2 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.15 `"Treat all Old as born in 1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.3 `"Born 1925-1940 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.45 `"Age 0-15 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.6 `"Drop Cohorts Born pre-1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.75 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.9 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -3.05 `"Drop GA w/ Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3.2 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

   gr_edit .yaxis1.add_ticks -3.35 `"Construction of MxFLS Risk Bins"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )

gr_edit .yaxis1.add_ticks -3.5 `"Collapse 5, 6 & 7"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3.65 `"Do not collapse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )


gr_edit .yaxis1.add_ticks -3.8 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -3.95 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -4.1 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -4.25 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -4.4 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -4.55 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
   
   


* now add stuff to the y axis  

gr_edit .yaxis1.add_ticks 0.25 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   
   
   if "`varinti'"=="SD" {

tw  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (rbar u95 l95 rank, fcolor(gs12) lcolor(gs12) barwidth(0.1) lwidth(none)) /// 95% CI
   (rbar u90 l90 rank, fcolor(gs6) lcolor(gs16) barwidth(0.1) lwidth(none))  /// 90% CI
   (scatter beta rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
   `scoff' `scon' /// indicators for spec
  (scatter beta rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
,legend (order(1 "Preferred spec." 4 "Point estimate" 2 "95% CI" 3 "90% CI") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(vsmall)) ///
   xtitle(" ") ytitle(" ") ///
   yscale(noline) xscale(noline) ylab(0(0.5)3, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white)) yline(0, lcolor(red))
   
   
   
   gr_edit .yaxis1.add_ticks -0.35 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -0.5 `"Treat all Old as born in 1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.65 `"Born 1925-1940 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.8 `"Age 0-15 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -0.95 `"Drop Cohorts Born pre-1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.1 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -1.25 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -1.4 `"Drop GA w/ Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -1.55 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )


   gr_edit .yaxis1.add_ticks -1.7 `"Construction of MxFLS Risk Bins"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )

gr_edit .yaxis1.add_ticks -1.85 `"Collapse 5, 6 & 7"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2 `"Do not collapse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )





gr_edit .yaxis1.add_ticks -2.15 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.3 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -2.45 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -2.6 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -2.75 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2.9 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
   
   
* now add stuff to the y axis  

gr_edit .yaxis1.add_ticks 3.25 `"Coefficient"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   
   
      if "`varinti'"=="sizeAvg" | "`varinti'"=="sizeSD" {

tw  (scatter se rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
   (scatter se rank if alter==1, mcolor(black) msymbol(o) msize(vsmall)) ///  point estimates
   `scoff' `scon' /// indicators for spec
  (scatter se rank if main==1, mcolor(orange) msymbol(o)  msize(vsmall)) ///  main spec 
,legend (order(1 "Preferred spec.") region(lcolor(white)) ///
	pos(12) ring(1) rows(1) size(vsmall) symysize(small) symxsize(small)) ///
   xtitle(" ") ytitle(" ") ///
   yscale(noline) xscale(noline) ylab(2000(2000)18000, noticks nogrid angle(horizontal)) xlab("", noticks)  ///
   graphregion (fcolor(white) lcolor(white)) plotregion(fcolor(white) lcolor(white))
   
   
   
      gr_edit .yaxis1.add_ticks 0 `"Treatment of Older Cohorts"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -1000 `"Treat all Old as born in 1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -2000 `"Born 1925-1940 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -3000 `"Age 0-15 Country Level Growth (Born 1925+)"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -4000 `"Drop Cohorts Born pre-1941"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -5000 `"Treatment of Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -6000 `"Keep all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )

gr_edit .yaxis1.add_ticks -7000 `"Drop GA w/ Observables"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -8000 `"Drop all Gamble Averse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )


   gr_edit .yaxis1.add_ticks -9000 `"Construction of MxFLS Risk Bins"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )

gr_edit .yaxis1.add_ticks -10000 `"Collapse 5, 6 & 7"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -11000 `"Do not collapse"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )




gr_edit .yaxis1.add_ticks -12000 `"Treatment of Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -13000 `"Include Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )

gr_edit .yaxis1.add_ticks -14000 `"Exclude Migrants"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) )) )


gr_edit .yaxis1.add_ticks -15000 `"Location of GDP Growth Experiences"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall) color(ebblue))) )
gr_edit .yaxis1.add_ticks -16000 `"Birth Province"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
gr_edit .yaxis1.add_ticks -17000 `"Province of Residence"', custom tickset(major) editstyle(tickstyle(textstyle(size(vsmall))) )
   
   

* now add stuff to the y axis  

gr_edit .yaxis1.add_ticks 20000 `"Sample Size"', custom tickset(major) editstyle(tickstyle(textstyle(size(small))) )	

   
   
   }
   



cd "$reploc/results"
graph export Figure5`varinti'.eps, replace

graph close

}

